package exsun.jt808.data.util.parse;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

public class PackageUtil {



    /**
     * 生成校验码
     * @param s
     * @return
     */
    public static int buildValidateCrc(byte[] s)
    {
        int r = 0;
        int len = s.length;
        for (int i = 0; i < len; i++)
        {
            r = s[i] ^ r;
        }
        return r;
    }

    // 数据转义
    // 7D-->7D01
    // 7E-->7D02
    public static byte[] buildTransferredRequest(byte[] buffers)
    {
            /**
              标识位：0x7e 消息中出现0x7e转义->0x7d 0x02；
	消息中出现0x7d转义为：0x7d 0x01；
	发送消息转义：消息封装->计算并填充检验码->转义
	接收消息转义：转义还原->验证检验码->解析消息

             */
        //数据转义
        List<Byte> bytes = new ArrayList<Byte>();

        for (int i = 0; i < buffers.length; i++)
        {
            if (buffers[i] == 0x7E)
            {
                bytes.add((byte) 0x7D);
                bytes.add((byte) 0x02);
            }
            else if (buffers[i] == 0x7D)
            {
                bytes.add((byte)0x7D);
                bytes.add((byte)0x01);
            }
            else
            {
                bytes.add(buffers[i]);
            }
        }
        byte[] b = new byte[bytes.size()];
        for(int j=0; j< bytes.size(); j++){
            b[j] = bytes.get(j);
        }
        return b;
    }

    /**
     * 接收消息时转义<br>
     *
     * <pre>
     * 0x7d01 <====> 0x7d
     * 0x7d02 <====> 0x7e
     * </pre>
     *
     * @param bs    要转义的字节数组
     * @param start 起始索引
     * @param end   结束索引
     * @return 转义后的字节数组
     * @throws Exception
     */
    public static byte[] doEscape4Receive(byte[] bs, int start, int end) throws Exception {
        if (start < 0 || end > bs.length)
            throw new ArrayIndexOutOfBoundsException(
                    "doEscape4Receive error : index out of bounds(start=" + start + ",end=" + end
                            + ",bytes length=" + bs.length + ")");
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();

            for (int i = 0; i < start; i++) {
                baos.write(bs[i]);
            }
            for (int i = start; i < end; i++) {
                if(i+1 >= bs.length){
                    baos.write(bs[i]);
                    continue;
                }
                if ((bs[i] == 0x7d) && (bs[i + 1] == 0x01)) {
                    baos.write(0x7d);
                    i++;
                } else if ((bs[i] == 0x7d) && (bs[i + 1] == 0x02)) {
                    baos.write(0x7e);
                    i++;
                } else {
                    baos.write(bs[i]);
                }
            }
            for (int i = end; i < bs.length; i++) {
                baos.write(bs[i]);
            }
            return baos.toByteArray();
        } catch (Exception e) {
            throw e;
        } finally {
            if (baos != null) {
                baos.close();
                baos = null;
            }
        }
    }
}
